<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/17 0017
 * Time: 上午 10:44
 */
namespace app\auth\controller;
use app\common\api\auth\IAuth;
use app\auth\validate\Vali_auth;
use app\auth\validate\ValidateFun;

class Auth implements IAuth {
    private static $config = [];
    /**
     * 初始化配置
     * @param array $config
     */
    public function __construct(array $config = [])
    {
        self::$config = array_filter_full(array_merge(self::$config,$config));
    }

    /**
     * 权限列表
     * @return mixed
     */
    public function auth_list(){
        $default =[
            'curr' =>1,
            'limits'=>10,
        ];
        $data = array_filter_full(array_merge($default,self::$config));
        #获取列表
        $list = new \app\auth\model\Auth();
        $result =$list->auth_list($data);
        #返回结果
        return ['code'=>200,'msg'=>config('msg.200'),'data'=>$result];
    }

    /**
     * 添加权限信息
     * @return mixed
     */
    public function auth_add(){
        $data = self::$config;
        unset($data['role_id']);
        #验证参数
        $vali = Vali_auth::add();
        $validate = new ValidateFun($vali['rule'],$vali['message']);
        if (!$validate->check($data)){
            $err = $validate->getError();
            return ['code'=>$err,'msg'=>config("msg.$err")];
        }
        #找出登录这信息
        $info = new \app\auth\model\Auth();
        $res = $info->auth_add($data);

        #返回结果
        if ($res>0){
            return ['code'=>200,'msg'=>config('msg.200'),'data'=>$res];
        }
        else{
            return ['code'=>10006,'msg'=>config('msg.10006')];
        }
    }


    /**
     * 更改/删除 权限信息
     * @return mixed
     */
    public function auth_update(){
        $data = self::$config;

        #验证参数
        if(isset($data['del'])){
            $vali = Vali_auth::auth_del();
        }else{
            $vali = Vali_auth::update();
        }
        if ($data['auth_pid']!=0){
            $vali1 = Vali_auth::update_ac();
            $validate = new ValidateFun($vali1['rule'],$vali1['message']);
            if (!$validate->check($data)){
                return ['code'=>$validate->getError(),'msg'=>config("msg.".$validate->getError())];
            }
        }
        $validate = new ValidateFun($vali['rule'],$vali['message']);
        if (!$validate->check($data)){
            return ['code'=>$validate->getError(),'msg'=>config("msg.".$validate->getError())];
        }


        #修改数据
        $update = new \app\auth\model\Auth();
        if(isset($data['del'])){
            $res = $update ->auth_del($data);
        }else{
            $res = $update->auth_update($data);
        }
        if ($res>0){
            return ['code'=>200,'msg'=>config('msg.200')];
        }else{
            return ['code'=>10025,'msg'=>config('msg.10025')];#数据未更新
        }
    }


}